#!/bin/bash

# 定义日志函数
function log_info() {
    echo "[INFO] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}

function log_result() {
    if [ $? -eq 0 ]; then
        echo "[SUCCESS] $(date '+%Y-%m-%d %H:%M:%S') - $1"
    else
        echo "[ERROR] $(date '+%Y-%m-%d %H:%M:%S') - $1"
        exit 1
    fi
}

# 1. 关闭SELinux
log_info "开始关闭SELinux..."
setenforce 0
log_result "临时关闭SELinux"

sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
log_result "永久禁用SELinux配置"

# 2. 关闭Swap
log_info "开始关闭Swap..."
swapoff -a
log_result "临时关闭Swap"

sed -i '/swap/s/^/#/' /etc/fstab
log_result "永久注释Swap配置"

# 3. 关闭Firewalld
log_info "开始关闭Firewalld..."
systemctl stop firewalld
log_result "停止Firewalld服务"

systemctl disable firewalld
log_result "禁用Firewalld开机启动"

# 4. 显示最终检查结果
echo -e "\n======= 最终检查结果 ======="
log_info "检查SELinux状态: $(getenforce)"
log_info "检查Swap状态: $(free -h | grep Swap | awk '{print $3}')"
log_info "检查Firewalld状态: $(systemctl is-active firewalld)"

echo -e "\n所有操作已完成！建议重启系统使更改生效。"
